import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 白名单
const whiteList = ['/login']

/**
 * to 即将进入的路由对象
 * from 即将离开的路由对象
 * next 跳转、放行
 */
router.beforeEach((to, from, next) => {
  NProgress.start()
  // 1. 如果已经登录不允许进入登录页面
  // 2. 如果没有登录，只能进入登录页面
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // next()
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
